package com.example.chesswithnet.Demo;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class Client {

    public static void main(String args[]) throws IOException {
        // InetAddress.getLocalHost()：获取本机服务
        // InetAddress.getByName("192.168.12.51")：获取指定ip地址的服务器
        Socket s = new Socket(InetAddress.getByName("192.168.12.59"),8080);
        // 客户端给服务端发送消息，主要通过IO流来实现
        // OutputStream：输出流
        OutputStream os = s.getOutputStream();
        // 通过write方法往流中写内容
        os.write("你好，服务端".getBytes());
        // 读取服务端发送的信息，通过IO流，输入流进行读取
        InputStream is = s.getInputStream();
        byte[] bytes = new byte[1024];
        int len = is.read(bytes);
        s.shutdownOutput(); //关闭
        System.out.println("信息发送成功");
//        System.out.println("对方回复："+new String(bytes,0,len,"UTF-8"));
    }

}
